<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"
        name="viewport">
  <title>浩然金融系统管理平台</title>
  <#include "/common/treelib.html">
  <style type="text/css">
    body {
      margin: 0;
    }
  </style>
  <script type="text/javascript">
      $(function () {
          $("#iframepage").css({"max-height": $(window).height() - 3, "overflow-y": "auto", "overflow-x": "hidden"});
      });

      $(window).resize(function () {
          $("#iframepage").css({"max-height": $(window).height() - 3, "overflow-y": "auto", "overflow-x": "hidden"});
      });

      $(function () {
          $("#leftTree").css({"max-height": $(window).height() - 3, "overflow-y": "auto", "overflow-x": "hidden"});
      });

      $(window).resize(function () {
          $("#leftTree").css({"max-height": $(window).height() - 3, "overflow-y": "auto", "overflow-x": "hidden"});
      });
  </script>
</head>
<body>
<div class="panel" id="leftTree" style="width:15%;border: 1px solid #e5e5e5;float:left;">
  <ul class="ztree" id="orgTree"></ul>
</div>
<div style="width:84%;height:100%;float:left;">
  <iframe id="iframepage" src="/securityOrg/query"
          style="background: white;border: 0; width: 100%; height: 520px; padding: 0;"></iframe>
</div>
</body>
<script type="text/javascript">
    $(function () {
        var demoTreeId = $("#orgTree");
        var treeName = "orgTree";
        var newCount = 1;
        var TreeManager = {
            /**
             *初始化方法
             */
            init: function () {
                /**
                 * 设置zTree树
                 */
                var setting = {
                    async: {
                        enable: false,
                        url: TreeManager.getUrlByNodeId,
                        dataFilter: TreeManager.filter
                    },
                    check: {
                        enable: false
                    },
                    data: {
                        simpleData: {
                            enable: true
                        }
                    },
                    view: {
                        addHoverDom: TreeManager.addHoverDom,
                        removeHoverDom: TreeManager.removeHoverDom,
                        selectedMulti: false
                    },
                    edit: {
                        enable: true
                    },
                    callback: {
                        beforeExpand: TreeManager.beforeExpand,
                        onAsyncSuccess: TreeManager.onAsyncSuccess,
                        onAsyncError: TreeManager.onAsyncError,
                        onClick: TreeManager.zTreeOnClick,
                        beforeRemove: TreeManager.beforeRemove,
                        beforeRename: TreeManager.beforeRename,
                        onDrop: TreeManager.dropOrg
                    }
                };
                var nodesJson = "${nodesJson!}";

                var zNodes = eval(nodesJson);
                $.fn.zTree.init(demoTreeId, setting, zNodes);//初始化树
            },
            /**
             * 功能：通过NodeId获得节点的孩子节点
             * 调用：当父节点展开时，调用，返回该父节点的子节点
             * 后台数据格式：JSON
             * @param treeId 树控件的Id
             * @param treeNode 树节点对象：包含Id等信息
             * @return
             */
            getUrlByNodeId: function (treeId, treeNode) {
                return "getNodesDataById?orgParentId=" + treeNode.id;
            },
            /**
             * 展开之前执行的函数
             * @param treeId
             * @param treeNode
             * @return
             */
            beforeExpand: function (treeId, treeNode) {
                if (!treeNode.isAjaxing) {
                    TreeManager.ajaxGetNodes(treeNode, "refresh");
                    return true;
                } else {
                    Jmts.alert("Loading...");
                    return false;
                }
            },
            /**
             * 加载成功后执行的函数
             * @param event 封装了js的事件
             * @param treeId 树控件的Id
             * @param treeNode 树节点对象
             * @param msg 返回的JSON格式的消息
             * @return
             */
            onAsyncSuccess: function (event, treeId, treeNode, msg) {
                if (!msg || msg.length == 0) {
                    return;
                }
                var zTree = $.fn.zTree.getZTreeObj(treeName);
                treeNode.icon = "";
                zTree.updateNode(treeNode);//更新树结构
                zTree.selectNode(treeNode.children[0]);//设置为第一个子节点为选中状态
            },
            onAsyncError: function (event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
                var zTree = $.fn.zTree.getZTreeObj(treeName);
                Jmts.alert("Error ! 异步获取数据异常");
                treeNode.icon = "";
                zTree.updateNode(treeNode);
            },
            ajaxGetNodes: function (treeNode, reloadType) {
                var zTree = $.fn.zTree.getZTreeObj(treeName);
                if (reloadType == "refresh") {
                    zTree.updateNode(treeNode);
                }
                zTree.reAsyncChildNodes(treeNode, reloadType, true);
            },
            /**
             * 功能：当点击树节点时，调用该函数
             * @param event
             * @param treeId
             * @param treeNode
             * @return
             */
            zTreeOnClick: function (event, treeId, treeNode) {
                var isMenu = treeNode.isMenu;
                //if(isMenu == 'N'){//当不是菜单的时候跳转页面
                document.getElementById("iframepage").src = "/securityOrg/to-orgTab?orgId=" + treeNode.id;
                //}
            },
            filter: function (treeId, parentNode, childNodes) {
                if (!childNodes) return null;
                for (var i = 0, l = childNodes.length; i < l; i++) {
                    childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
                }
                return childNodes;
            },
            beforeRemove: function (treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj("orgTree");
                zTree.selectNode(treeNode);
                if (treeNode.pId == null || treeNode.pId == '') {
                    Jmts.alert("根节点不能删除");
                    return false;
                }
                if (confirm("确认删除节点 -- " + treeNode.name + "--吗？")) {
                    var url = "/securityOrg/delete"
                    var data = {
                        orgId: treeNode.id
                    }
                    Jmts.ajaxPost(url, data, function (result) {
                        if (result.success) {
                            window.location.reload();
                            return true;
                        } else {
                            Jmts.warnTips(result.msg);
                            return false;
                        }
                    });
                } else {
                    return false;
                }
            },
            beforeRename: function (treeId, treeNode, newName) {
                if (newName.length == 0) {
                    setTimeout(function () {
                        var zTree = $.fn.zTree.getZTreeObj("orgTree");
                        zTree.cancelEditName();
                        Jmts.alert("节点名称不能为空.");
                    }, 0);
                    return false;
                }
                if (treeNode.name == newName) {
                    setTimeout(function () {
                        var zTree = $.fn.zTree.getZTreeObj("orgTree");
                        zTree.cancelEditName();
                    }, 0);
                    return true;
                }
                var url = "/securityOrg/updateOrgName";
                var data = {
                    orgId: treeNode.id,
                    orgName: newName
                }
                Jmts.ajaxPost(url, data, function (result) {
                    if (result.success) {
                        window.location.reload();
                        return true;
                    } else {
                        Jmts.warnTips(Jmts.msg);
                        return false;
                    }
                })
            },
            addHoverDom: function (treeId, treeNode) {
                var sObj = $("#" + treeNode.tId + "_span");
                if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
                var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
                    + "' title='添加节点' onfocus='this.blur();' treenode_add></span>";
                sObj.after(addStr);
                var btn = $("#addBtn_" + treeNode.tId);
                if (btn) btn.bind("click", function () {
                    var count = newCount++;
                    var url = "/securityOrg/saveSecurityOrg";
                    var data = {
                        operation: 'add',
                        orgParentId: treeNode.id,
                        orgCode: 'org_' + count,
                        orgName: "新节点" + count,
                        orgType: "01",
                        orgComment: "新节点" + count
                    }
                    Jmts.ajaxPost(url, data, function (result) {
                        if (result.success) {
                            var zTree = $.fn.zTree.getZTreeObj("orgTree");
                            zTree.addNodes(treeNode, {id: (100 + newCount), pId: treeNode.id, name: "新节点" + count});
                            window.location.reload();
                        } else {
                            Jmts.warnTips(result.msg);
                        }
                    })
                    return false;
                });
            },
            removeHoverDom: function (treeId, treeNode) {
                $("#addBtn_" + treeNode.tId).unbind().remove();
            },
            //移动树菜单节点
            dropOrg: function (event, treeId, treeNodes, targetNode, moveType) {
                var id = treeNodes[0].id;//获得被拖拽的节点id
                var targetId = targetNode.id;//获得目标id
                var url = "/securityOrg/updateOrgParentId";
                var data = {
                    id: id,
                    orgParentId: targetId
                }
                Jmts.ajaxPost(url, data, function (reuslt) {
                    if (!result.success) {
                        Jmts.warnTips(result.msg);
                        window.location.reload();
                    }
                })
            }
        };
        TreeManager.init();
    });
</script>
</html>
